<template>
  <com-card>
    <div slot="com_header">
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入商品名称"
            v-model="goodsObj.query"
            class="input-with-select"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="query"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4" style="margin-left: 20px">
          <el-button @click="jump" type="primary" icon="el-icon-plus"
            >添加</el-button
          >
        </el-col>
      </el-row>
    </div>
    <div slot="com_content">
      <el-row>
        <el-table :data="tableData" border>
          <el-table-column type="index" label="序号"> </el-table-column>
          <el-table-column prop="goods_name" label="商品名称" width="300px">
          </el-table-column>
          <el-table-column prop="goods_price" label="商品价格">
          </el-table-column>
          <el-table-column prop="goods_number" label="商品数量">
          </el-table-column>
          <el-table-column prop="add_time" label="创建时间"> </el-table-column>
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-tooltip content="修改商品" placement="top">
                <el-button
                  size="small"
                  type="primary"
                  icon="el-icon-edit"
                  @click="xgquery(row)"
                ></el-button>
              </el-tooltip>
              <el-tooltip content="删除商品" placement="top">
                <el-button
                  size="small"
                  type="danger"
                  icon="el-icon-delete"
                  @click="del(row)"
                ></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="goodsObj.pagenum"
          :page-sizes="[3, 5, 7, 9]"
          :page-size="goodsObj.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </el-row>
      <!-- 修改弹窗 -->
      <el-dialog title="提示" :visible.sync="dialogbj" width="30%">
        <el-form :model="xgobj" label-width="100px">
          <el-form-item label="商品名称">
            <el-input v-model="xgobj.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="价格">
            <el-input v-model="xgobj.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="数量">
            <el-input v-model="xgobj.goods_number"></el-input>
          </el-form-item>
          <el-form-item label="重量">
            <el-input v-model="xgobj.goods_weight"></el-input>
          </el-form-item>
          <el-form-item label="分类列表" prop="goods_cat">
            <div class="text item">
              <div class="block">
                <el-cascader
                  v-model="value"
                  :options="listobj"
                  :props="{
                    expandTrigger: 'hover',
                    value: 'cat_id',
                    label: 'cat_name',
                  }"
                  @change="handleChange"
                ></el-cascader>
              </div>
            </div>
          </el-form-item>
          <el-form-item label="介绍">
            <el-input v-model="xgobj.goods_introduce"></el-input>
          </el-form-item>
          <el-form-item label="上传的图片">
            <el-upload
              class="avatar-uploader"
              action="http://127.0.0.1:8888/api/private/v1/"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="商品的参数">
            <el-input v-model="xgobj.attrs"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogbj = false">取 消</el-button>
          <el-button type="primary" @click="deleng()">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </com-card>
</template>

<script>
import ComCard from "../../components/comCard";
import { api_goods, api_goodsadd, api_delete,api_categories } from "../../api/goods.js";
export default {
  components: {
    ComCard,
  },
  data() {
    return {
      goodsObj: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      //总数据列表
      tableData: [],
      //总数据条数
      total: 0,
      //修改
      xgobj: {},
      //修改弹窗
      dialogbj: false,
      imageUrl: "", //编辑图片地址
      listobj:{},
      value:""
    };
  },
  mounted() {
    this.query();
    this.bjbj();
  },
  methods: {
    //查询
    async query() {
      let rst = await api_goods(this.goodsObj);
      console.log(rst);
      let { total, goods } = rst.data;
      this.total = total;
      this.tableData = goods;
    },
    async bjbj() {
      let rst = await api_categories(this.fenleilist);
      this.listobj = rst.data;
    },
    //当页发生变化触发的函数
    handleCurrentChange(pagenum) {
      this.goodsObj.pagenum = pagenum;
      this.query();
    },
    //当条数发生变化触发的函数
    handleSizeChange(pagesize) {
      this.goodsObj.pagesize = pagesize;
      this.query();
    },

    //添加按钮跳转
    jump() {
      this.$router.push("./goodsJump");
    },
    //修改
    xgquery(row) {
      this.dialogbj = true;
      let {
        goods_id,
        goods_name,
        goods_price,
        goods_number,
        goods_weight,
        goods_cat,
        goods_introduce,
        attrs,
      } = row;
      this.xgobj = {
        goods_id,
        goods_name,
        goods_price,
        goods_number,
        goods_weight,
        goods_cat,
        goods_introduce,
        attrs,
      };
    },
    handleChange(val) {
      this.xgobj.goods_cat = val.join(",");
      console.log(this.xgobj.goods_cat);
    },
    //编辑图片
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    //修改弹窗确定按钮
    async deleng() {
      this.dialogbj = false;
      let rst = await api_goodsadd(this.xgobj);
      console.log(rst);
      let { msg, status } = rst.meta;
      if (status == 200) {
        this.$message.success(msg);
        this.query();
      }
    },
    //删除
    del(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(
        async () => {
          let rst = await api_delete({ id: row.goods_id });
          let { status, msg } = rst.meta;
          if (status == 200) {
            this.$message.success(msg);
            this.query();
          }
        },
        () => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        }
      );
    },
  },
};
</script>

<style lang="less" scoped>
</style>